package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.system.dao.PromptingLanguageRepository;
import com.ssy.lingxi.system.dto.request.PromptingLanguageDeleteRequest;
import com.ssy.lingxi.system.dto.request.PromptingLanguageListRequest;
import com.ssy.lingxi.system.entity.PromptingLanguage;
import com.ssy.lingxi.system.service.PromptingLanguageService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class PromptingLanguageServiceImpl implements PromptingLanguageService {


    @Resource
    PromptingLanguageRepository promptingLanguageRepository;

    /**
     * 查询提示语列表
     * @param request
     * @author wrc
     * @date 2020/6/9
     */
    @Override
    public Wrapper<PageData<PromptingLanguage>> promptingLanguageList(PromptingLanguageListRequest request) {
        PromptingLanguage promptingLanguage=new PromptingLanguage();
        promptingLanguage.setReturnCodeId(request.getReturnCodeId());
        Example<PromptingLanguage> example =Example.of(promptingLanguage);
        Page<PromptingLanguage> all = promptingLanguageRepository.findAll(example,PageRequest.of(request.getCurrent(), request.getPageSize(), Sort.by("id").ascending()));
        return Wrapper.success(new PageData<>(all.getTotalElements(),all.getContent()));
    }

    /**
     * 删除
     *
     * @param request
     * @author wrc
     * @date 2020/6/9
     */
    @Transactional
    @Override
    public Wrapper promptingLanguageDelete(PromptingLanguageDeleteRequest request) {
        promptingLanguageRepository.deleteById(request.getId());
        return Wrapper.success();
    }

    /**
     * 修改
     *
     * @param request
     * @author wrc
     * @date 2020/6/9
     */
    @Transactional
    @Override
    public Wrapper promptingLanguageUpdate(PromptingLanguage request) {
        promptingLanguageRepository.save(request);
        return Wrapper.success();
    }

    /**
     * 添加
     *
     * @param request
     * @author wrc
     * @date 2020/6/9
     */
    @Transactional
    @Override
    public Wrapper promptingLanguageAdd(PromptingLanguage request) {
        promptingLanguageRepository.save(request);
        return Wrapper.success();
    }
}
